class Robot3 extends egret.DisplayObjectContainer {

    constructor() {
        super();
        this.armatureDisplayInit();
        this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
        this.stand();
    }

    public stage;
    public armatureDisplay:dragonBones.EgretArmatureDisplay;
    public moveDistanceBase:number = 9;
    public movePreDistanceBase:number = 4;
    private moveDistance:number = 0;
    public directionIsLeft:boolean = true;
    private isJumping:boolean = false;
    public state = 'stand';
    public STATE_STAND: string = 'stand';
    public STATE_WALK_PRE: string = 'walk_pre';
    public STATE_WALK: string = 'walk';
    public STATE_JUMP: string = 'jump';
    private EARTHQUAKE_EVENT_NAME:string = 'quake';
    private WALKSTART_EVENT_NAME:string = 'walkstart';
    private WALKEND_EVENT_NAME:string = 'walkend'

    public armatureDisplayInit() {
        var robotDragonbonesData = RES.getRes('robot_3_ske_json');
        var robotData = RES.getRes('robot_3_tex_json');
        var robotTexture = RES.getRes('robot_3_tex_png');
        let egretFactory: dragonBones.EgretFactory = dragonBones.EgretFactory.factory;
        egretFactory.parseDragonBonesData(robotDragonbonesData);
        egretFactory.parseTextureAtlasData(robotData, robotTexture);
        this.armatureDisplay = egretFactory.buildArmatureDisplay("robot");
        this.armatureDisplay.addEventListener(dragonBones.EventObject.FRAME_EVENT, this.onWalkStep, this);
        this.armatureDisplay.addEventListener(dragonBones.EventObject.LOOP_COMPLETE, this.onJumpEnd, this);
        this.addChild(this.armatureDisplay);
    }

    public moveToLeft():void {
        this.scaleX = this.scaleX>0 ? this.scaleX : -this.scaleX;
        this.armatureDisplay.animation.play(this.STATE_WALK_PRE);
        this.directionIsLeft = true;
        this.state = this.STATE_WALK;
    }

    public moveToRight():void {
        this.scaleX = this.scaleX<0 ? this.scaleX : -this.scaleX;
        this.armatureDisplay.animation.play(this.STATE_WALK_PRE);
        this.directionIsLeft = false;
        this.state = this.STATE_WALK;
    }

    public stand():void {
        this.armatureDisplay.animation.play(this.STATE_STAND);
        this.state = this.STATE_STAND;
    }

    public jump():void {
        if(this.state === this.STATE_JUMP) {
            return;
        }
        this.armatureDisplay.animation.play(this.STATE_JUMP);
        this.state = this.STATE_JUMP;
    }

    public onWalkStep(event: dragonBones.EgretEvent) {
        //踩地
        if( event.eventObject.name === this.EARTHQUAKE_EVENT_NAME ) {
            MainSenceManager.mainSenceEarthQuake();
        }
        //移动开始
        if( event.eventObject.name === this.WALKSTART_EVENT_NAME ) {
            if(event.animationName === this.STATE_WALK_PRE) {
                //抬腿起步
                this.moveDistance = this.movePreDistanceBase;
            }else {
                //正常迈步
                this.moveDistance = this.moveDistanceBase;
            }
        }
        //移动结束
        if( event.eventObject.name === this.WALKEND_EVENT_NAME ) {
            this.moveDistance = 0;
        }
    }

    public onJumpEnd(event: dragonBones.EgretEvent) {
        console.log(event.animationName)
        if( event.animationName === this.STATE_JUMP ) {
            this.stand();
        }
    }

    protected update() {
        switch (this.state) {
            case this.STATE_STAND:
                break;

            case this.STATE_WALK:
                let d:number;
                if(this.directionIsLeft) {
                    d = this.moveDistance * -1;
                }else {
                    d = this.moveDistance;
                }
                this.x += d;
                break;

            case this.STATE_JUMP:
                break;
        }
    }
}